/*
 * Copyright (c) 2013 WDCY Information Technology Co. Ltd
 * www.wdcy.cc
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * WDCY Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with WDCY Information Technology Co. Ltd.
 */
package com.monkey.web.platform.controller;

import com.monkey.core.domain.resource.Menu;
import com.monkey.core.plugin.dto.MonkeyDto;
import com.monkey.core.web.response.MonkeyResponseDto;
import com.monkey.web.platform.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Wu Tianqiang
 */
@Controller
@RequestMapping(value = "/m", produces = {MediaType.APPLICATION_JSON_VALUE})
public class ResourceController {

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/menu", method = RequestMethod.POST)
    public String addMenu(MonkeyDto monkeyDto) {
        monkeyDto.println();
        menuService.addMenu(monkeyDto);
        return "SUCCESS:新增菜单成功";
    }

    @RequestMapping(value = "/menu", method = RequestMethod.PUT)
    public String modifyMenu(MonkeyDto monkeyDto) {
        monkeyDto.println();
        menuService.modifyMenu(monkeyDto);
        return "SUCCESS:修改菜单成功";
    }

    @RequestMapping(value = "/menu/{ids}", method = RequestMethod.DELETE)
    public String removeMenu(@PathVariable Long[] ids) {
        menuService.removeMenu(ids);
        return "SUCCESS:删除菜单成功";
    }

    @RequestMapping(value = "/child_menus", method = RequestMethod.GET)
    public List<Menu> childMenus(@RequestParam Long parentId) {
        return menuService.findChildByParent(parentId, false);
    }

    @RequestMapping(value = "/children_menus", method = RequestMethod.GET)
    public MonkeyResponseDto childrenMenus(@RequestParam Long parentId) {
        MonkeyResponseDto responseDto = new MonkeyResponseDto("children");
        return responseDto.success(menuService.findChildren(parentId, false).getChildren());
    }

    @RequestMapping(value = "/menu_list", method = RequestMethod.GET)
    public MonkeyResponseDto menu_list(@RequestParam Long parentId) {
        MonkeyResponseDto responseDto = new MonkeyResponseDto("children");
        return responseDto.success(menuService.findChildren(parentId, true));
    }


}